#include "KeyManagerWidget.h"
#include "Command.h"
#include "KeyViewWidget.h"
#include "CreateKeyWidget.h"
#include "Controller.h"

#include <QMenuBar>
#include <QAction>

using namespace kryptos::controller;

namespace kryptos { namespace ui {

KeyManagerWidget::KeyManagerWidget(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    m_menuBar = new QMenuBar(ui.wMenu);
    m_menuBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    ui.wMenu->layout()->addWidget(m_menuBar);

    m_goBackAction      = m_menuBar->addAction("< Back"      , this, SLOT(showKeyView()));
    // General menu
    m_createKeyAction   = m_menuBar->addAction("New"         , this, SLOT(showCreateKey()));
    m_importKeyAction   = m_menuBar->addAction("Import"      , this, SLOT(showImportKey()));
    m_createGroupAction = m_menuBar->addAction("Create Group", this, SLOT(showCreateGroup()));
    // Key Context menu
    m_editKeyAction     = m_menuBar->addAction("Edit Key"    , this, SLOT(showEditKey()));
    m_shareKeyAction    = m_menuBar->addAction("Share Key"   , this, SLOT(showShareKey()));
    m_deleteKeyAction   = m_menuBar->addAction("Delete Key"  , this, SLOT(showDeleteKey()));
    // Group context menu
    m_editGroupAction   = m_menuBar->addAction("Edit Group"  , this, SLOT(showEditGroup()));
    m_deleteGroupAction = m_menuBar->addAction("Delete Group", this, SLOT(showDeleteGroup()));

    hideAllMenues();
    showGeneralMenu();

    // Default view shows user keys, groups and contacts
    showKeyView();

    connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(executeCommand()));
    connect(&Controller::instance(), SIGNAL(keyOperationFinished()), this, SLOT(showKeyView()));
}

void KeyManagerWidget::setCreateKeyCommand(Command* cmd)
{
    Q_CHECK_PTR(cmd);
    m_createKeyCmd = cmd;
}

void KeyManagerWidget::setShareKeyCommand(Command* cmd)
{
    Q_CHECK_PTR(cmd);
    m_shareKeyCmd = cmd;
}

void KeyManagerWidget::hideGeneralMenu()
{
    m_createKeyAction->setVisible(false);
    m_importKeyAction->setVisible(false);
    m_createGroupAction->setVisible(false);
}

void KeyManagerWidget::showGeneralMenu()
{
    m_createKeyAction->setVisible(true);
    m_importKeyAction->setVisible(true);
    m_createGroupAction->setVisible(true);
}

void KeyManagerWidget::hideAllMenues()
{
    hideGeneralMenu();
    m_goBackAction->setVisible(false);
    m_editKeyAction->setVisible(false);
    m_shareKeyAction->setVisible(false);
    m_deleteKeyAction->setVisible(false);
    m_editGroupAction->setVisible(false);
    m_deleteGroupAction->setVisible(false);
}

template<class Type> void KeyManagerWidget::createWidget()
{
    if (!m_currentWidget.isNull())
    {
        ui.wContent->layout()->removeWidget(m_currentWidget.data());
        m_currentWidget->hide();
    }

    m_currentWidget.reset(new Type(ui.wContent));

    ui.wContent->layout()->addWidget(m_currentWidget.data());
    m_currentWidget->show();
}

void KeyManagerWidget::showKeyView()
{
    hideAllMenues();
    showGeneralMenu();
    m_currentMenu = KeyView;
    createWidget<KeyViewWidget>();

    ((KeyViewWidget*)m_currentWidget.data())->fillIdentitiesAndGroups(Controller::instance().contacts());
}

void KeyManagerWidget::showCreateKey()
{
    hideGeneralMenu();
    m_goBackAction->setVisible(true);
    m_currentMenu = CreateKey;
    createWidget<CreateKeyWidget>();
}

void KeyManagerWidget::showImportKey()
{

}

void KeyManagerWidget::showCreateGroup()
{

}

void KeyManagerWidget::showEditKey()
{

}

void KeyManagerWidget::showShareKey()
{

}

void KeyManagerWidget::showDeleteKey()
{

}

void KeyManagerWidget::showEditGroup()
{

}

void KeyManagerWidget::showDeleteGroup()
{

}

void KeyManagerWidget::executeCommand()
{
    QMap<QString, QString> params = m_currentWidget->params();

    switch (m_currentMenu)
    {
        case CreateKey: m_createKeyCmd->execute(params);break;
        case ShareKey : m_shareKeyCmd->execute(params);break;
    }
}

}}
